﻿using System;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.Linq;
using System.Windows;
using PairingMatrix.Common.Command;
using PairingMatrix.Model;

namespace PairingMatrix.ViewModel
{
    public class AbsenteesViewModel : ViewModelBase
    {
        private readonly ObservableCollection<ITeamMemberCardViewModel> _teamMembers;
        public ObservableCollection<ITeamMemberCardViewModel> AbsentTeamMembers { get; private set; }
        private const int INITIAL_Y = 610;

        public AbsenteesViewModel(AbsenteesModel absenteesModel, ObservableCollection<ITeamMemberCardViewModel> teamMembers)
        {
            _teamMembers = teamMembers;
            AbsentTeamMembers = new ObservableCollection<ITeamMemberCardViewModel>();
            absenteesModel.TeamMemberModels.CollectionChanged += TeamMemberModels_CollectionChanged;

        }


        void TeamMemberModels_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
        {
            if(e.Action == NotifyCollectionChangedAction.Add)
            {
                var teamMemberModel = (TeamMemberModel)e.NewItems[0];
                AddTeamMemberInNextAvailablePosition(teamMemberModel.Id);
            }
            else if(e.Action == NotifyCollectionChangedAction.Remove)
            {
                var teamMember = AbsentTeamMembers.FirstOrDefault(t => t.Id == ((TeamMemberModel) e.OldItems[0]).Id);
                if (teamMember != null)
                {
                    AbsentTeamMembers.Remove(teamMember);
                    teamMember.ShowCardInAbsenteesBox = false;
                }

                //to rearrange all again
                var copy = AbsentTeamMembers.Select(t => t).ToList();
                AbsentTeamMembers.Clear();
                copy.ForEach(newTeamMember => AddTeamMemberInNextAvailablePosition(newTeamMember.Id));
            }
        }

        private void AddTeamMemberInNextAvailablePosition(Guid teamMemberId)
        {
            for(int y = INITIAL_Y; y <= INITIAL_Y + 1600; y+=129)
            {
                int x = 20;
                while (AbsentTeamMembers.Any(t => t.Position.X == x && t.Position.Y == y))
                {
                    x += 110;
                }

                if (x >= 350)
                    continue;

                var teamMemberCardViewModel = _teamMembers.First(t => t.Id == teamMemberId);
                teamMemberCardViewModel.ShowCardInAbsenteesBox = true;
                var position = new Point(x, y);
                AbsentTeamMembers.Add(teamMemberCardViewModel);
                teamMemberCardViewModel.Position = position;
                break;
            }
        }

        private RelayCommand _toggleAbsenteesBoxVisibilityCommand;
        public RelayCommand ToggleAbsenteesBoxVisibilityCommand
        {
            get
            {
                return _toggleAbsenteesBoxVisibilityCommand ??
                       (_toggleAbsenteesBoxVisibilityCommand = new RelayCommand(param => ShowAbsenteesBox = !ShowAbsenteesBox));
            }
        }

        private bool _showAbsenteesBox;
        public bool ShowAbsenteesBox
        {
            get { return _showAbsenteesBox; }
            set
            {
                if (_showAbsenteesBox == value)
                    return;
                _showAbsenteesBox = value;
                OnPropertyChanged("ShowAbsenteesBox");
            }
        }

    }
}